﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage2 : Stage
    {
        public Stage2(Game game) : base(game) { }

        float GROUND_ALPHA = 0f;
        public override void InitializeScenes()
        {
            base.InitializeScenes();

            Animation title = new Animation("images/title/stageTitle", new Rectangle(2, 152, 184, 36), new Point(184, 36));
            title.Scale = 1.2f;
            Transition tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(420, 425), 0, 1, 80, 220);//0, 366
            tra.Register(this);

            for (int i = 1; i < 3; i++)
            {
                Animation a = new Animation("images/stages/stage2/city0" + i);
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }
            _backGroundPos = new Vector2(-400, -360);

            _scenceBound = new Rectangle((int)(_backGroundPos.X + 380), (int)(_backGroundPos.Y + 480),
                 1450,
                550);

            camera.Bound = _scenceBound;

            for (int i = 0; i < 2; i++)
            {
                Animation wallBack = new Animation("images/stages/stage2/stage2_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-321, -128);

            GroundObject grount =
            StateFactory.CreateGround(this, 57, 445, 384, 60, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 428, 477, 376, 31, -0.25f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 696, 525, 172, 33, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 865, 444, 166, 195, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 201, 346, 100, 15, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 497, 377, 100, 15, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 660, 320, 100, 15, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, -262, 182, 256, 587, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 45, 201, 358, 15, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 349, 273, 100, 15, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatePortal("Stage2_Protal_1", this, 0, 365, "stage1_1", "Stage1_1_Portal_3"); grount =
            StateFactory.CreateGround(this, 988, 469, 80, 145, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1068, 494, 80, 95, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1148, 519, 80, 45, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1073, 316, 106, 15, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1446, 280, 200, 112, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatePortal("Stage2_Protal_2", this, 1454, 450, "stage2_1", "Stage2_1_Portal_1"); grount =
            StateFactory.CreateGround(this, 1399, 519, 422, 45, 0f); grount.Alpha = GROUND_ALPHA; 
            StateFactory.CreatePortal("Stage2_Protal_3", this, 1456, 174, "stage2", "Stage2_Protal_1");
            StateFactory.CreateKey("Stage2_Key_S1", this, -47, 168, "Stage2_door_1");
            
            GameDoor door=
            StateFactory.CreateDoor("Stage2_door_S1", this, 752, 458, "Stage2_door_1");
            door.OpenDoorAction += this.Cake;

            grount =
            StateFactory.CreateGround(this, 1582, 77, 70, 298, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1525, 422, 42, 172, 0f); grount.Alpha = GROUND_ALPHA; 
            StateFactory.CreateElementPiece("Stage2_Ele_1", this, 675, 297);
            StateFactory.CreateElementPiece("Stage2_Ele_2", this, 880, 240);
            SensorSwitch sensorSwitch = 
            StateFactory.CreateSensorSwitch(this, 880, 334);

            Mechine mechine =
            StateFactory.CreateMechine("Stage2_Mechine_1", this, 1267, 456, 2); 
            MechanismGound mechanismGound =
            StateFactory.CreateMechanismGound(this, 1369, 416, 42, 160, 0, 0, -60, 0, 45); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage2", new Rectangle(2, 19, 42, 160), new Point(42, 160)); 
            mechanismGound =
            StateFactory.CreateMechanismGound(this, 1233, 316, 118, 15, 0, 0, -56, 0, 30); sensorSwitch.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage2", new Rectangle(2, 2, 118, 15), new Point(118, 15));
            


            Font = ResourceManager.LoadSpriteFont("SpriteFont1");
            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f,false);
            MusicManager.AddMusic("Music/cake", "cake", 0.001f, false);
            _song = MusicManager.LoadSong("Music/stage2");
            GameClass._musicString = "Music/stage2";
            MediaPlayer.Play(_song);

            _startProtal = new ProtalCommand("stage2", "Stage2_Protal_1");

        }


        private void Cake()
        {
            //throw new Exception();
            UpdateAction += this.MakeCake;
        }

        private void MakeCake()
        {
            Animation cakeAni = new Animation("images/system/cake");
            Image cake = new Image(cakeAni);
            cake.Position = new Vector2(752, 458);
            AlphaModifier ap = new AlphaModifier(0, 1, 1, 15);
            PositionModifier p = new PositionModifier(cake.Position, cake.Position + new Vector2(0,-60), 1, 25);
            ModifierContain mc = new ModifierContain(cake);
            mc.AddModifier(p);
            mc.AddModifier(ap);
            cake.ModifierContainer = mc;
            actors.Add(cake);
            MusicManager.PlayMusic("cake");
        }

        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            base.OnDraw(spriteBatch, delta);
            //GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();

            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "" + test, new Vector2(50, 50), Color.Black);
            //spriteBatch.End();
        }

        public override void Update(int delta)
        {
            base.Update(delta);
        }

        public override Stage Clone()
        {
            Stage2 stage = new Stage2(Game);
            stage.InitializeScenes();
            return stage;
        }

        public override void Dispose()
        {
            base.Dispose();
            //mic.Dispose();
        }
    }
}
